/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * buscarUsuario.java
 *
 * Created on 22-abr-2011, 17:34:17
 */

package interfaces;

import clinica.ClinicaView;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ListDataListener;
import org.jdesktop.application.Action;
import Controlador.*;
import GestorUsuarios.EstadoUsuario;
import GestorUsuarios.Rol;
import GestorUsuarios.TipoUsuario;
import GestorUsuarios.Usuario;
import clinica.Interfaces;
import clinica.Interfaz;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author victor
 */
public class buscarUsuario extends javax.swing.JPanel {

    private ClinicaView app;
    private Rol [] roles;
    /** Creates new form buscarUsuario */
    public buscarUsuario(ClinicaView a) {
        app = a;
        initComponents();
    }

    public void limpiaDatos() {
        nombre.setText("");
        apellidos.setText("");
        dni.setText("");
        tipousu.setSelectedIndex(0);
        tipoper.setSelectedIndex(0);
        tipoper.setEnabled(false);
        tipomed.setSelectedIndex(0);
        tipomed.setEnabled(false);
        estado.setSelectedIndex(0);

    }

    public void rellenaRoles() throws SQLException, IOException, ClassNotFoundException {
        roles = Rol.getTodos();

        int cuenta;
        if(roles == null)
            cuenta = 0;
        else cuenta = roles.length;

        String [] nombre_roles = new String[cuenta+1];
        nombre_roles[0] = "No usar";
        for(int i=1;i<=cuenta;i++)
            nombre_roles[i] = roles[i-1].getNombre();
        tipomed.setModel(new DefaultComboBoxModel(nombre_roles));
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        nombre = new javax.swing.JTextField();
        apellidos = new javax.swing.JTextField();
        dni = new javax.swing.JTextField();
        tipousu = new javax.swing.JComboBox();
        tipoper = new javax.swing.JComboBox();
        tipomed = new javax.swing.JComboBox();
        estado = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        cancel = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(buscarUsuario.class);
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel1.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize())); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 720, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 13, Short.MAX_VALUE)
        );

        jLabel2.setFont(Interfaz.setFontSize());
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(Interfaz.setFontSize());
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(Interfaz.setFontSize());
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(Interfaz.setFontSize());
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setFont(Interfaz.setFontSize());
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setFont(Interfaz.setFontSize());
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setFont(Interfaz.setFontSize());
        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jPanel3.setName("jPanel3"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 744, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 24, Short.MAX_VALUE)
        );

        nombre.setFont(Interfaz.setFontSize());
        nombre.setName("nombre"); // NOI18N

        apellidos.setFont(Interfaz.setFontSize());
        apellidos.setName("apellidos"); // NOI18N

        dni.setFont(Interfaz.setFontSize());
        dni.setName("dni"); // NOI18N

        tipousu.setFont(Interfaz.setFontSize());
        tipousu.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "No usar", "Paciente", "Personal clínica" }));
        tipousu.setName("tipousu"); // NOI18N
        tipousu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tipousuActionPerformed(evt);
            }
        });

        tipoper.setFont(Interfaz.setFontSize());
        tipoper.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Médico", "Farmacéutico", "Gestor", "Técnico de pruebas" }));
        tipoper.setName("tipoper"); // NOI18N
        tipoper.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tipoperActionPerformed(evt);
            }
        });

        tipomed.setFont(Interfaz.setFontSize());
        tipomed.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nulo" }));
        tipomed.setName("tipomed"); // NOI18N

        estado.setFont(Interfaz.setFontSize());
        estado.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "No usar", "Activo", "Baja temporal", "Baja definitiva" }));
        estado.setName("estado"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(apellidos, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                            .addComponent(nombre, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                            .addComponent(dni, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                            .addComponent(tipousu, 0, 530, Short.MAX_VALUE)
                            .addComponent(tipoper, 0, 530, Short.MAX_VALUE)
                            .addComponent(tipomed, 0, 530, Short.MAX_VALUE)
                            .addComponent(estado, 0, 530, Short.MAX_VALUE))))
                .addContainerGap())
            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(apellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(dni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tipousu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(tipoper, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(tipomed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(estado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(buscarUsuario.class, this);
        jButton1.setAction(actionMap.get("aceptar")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        cancel.setIcon(resourceMap.getIcon("cancel.icon")); // NOI18N
        cancel.setName("cancel"); // NOI18N
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(cancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addComponent(cancel))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tipousuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tipousuActionPerformed
        if(tipousu.getSelectedIndex()!=2) {
            tipoper.setEnabled(false);
            tipomed.setEnabled(false);
        } else {
            tipoper.setEnabled(true);
            if(tipoper.getSelectedIndex()==0)
                tipomed.setEnabled(true);
        }
    }//GEN-LAST:event_tipousuActionPerformed

    private void tipoperActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tipoperActionPerformed
        if(tipoper.getSelectedIndex()==0)
                tipomed.setEnabled(true);
        else tipomed.setEnabled(false);
    }

    public String getApellidos() {
		if("".equals(apellidos.getText()))
			return null;
		else return apellidos.getText();
	}

	public String getNombre() {
		if("".equals(nombre.getText()))
			return null;
		else return nombre.getText();
	}

	public String getDni() {
		if("".equals(dni.getText()))
			return null;
		else return dni.getText();
	}

	public TipoUsuario getTipoUsuario() {
                if(tipousu.getSelectedIndex()==0)
                    return null;

		if(tipousu.getSelectedIndex()==1) return TipoUsuario.PACIENTE;
		if(tipousu.getSelectedIndex()==2) {
                    if(tipoper.getSelectedIndex()==0) return TipoUsuario.MEDICO;
                    if(tipoper.getSelectedIndex()==1) return TipoUsuario.FARMACEUTICO;
                    if(tipoper.getSelectedIndex()==2) return TipoUsuario.GESTOR;
                    else return TipoUsuario.TECNICOPRUEBAS;
                }

                return null;
	}

	public EstadoUsuario getEstado() {
		if(estado.getSelectedIndex()==0) return null;
		else if(estado.getSelectedIndex()==1) return EstadoUsuario.ACTIVO;
		else if(estado.getSelectedIndex()==2) return EstadoUsuario.BAJA_TEMPORAL;
		else return EstadoUsuario.BAJA_DEFINITIVA;
	}


	public Rol getRol() {
            if(tipomed.getSelectedIndex()==0) {
                return null;
            } else {
                return roles[tipomed.getSelectedIndex()];
            }
        }

    @Action
    public void aceptar() throws NoSuchAlgorithmException, IOException, ClassNotFoundException  {
        Usuario [] us = Controlador.buscarUsuarios(null, null, getDni(), getNombre(), getApellidos(), getTipoUsuario(), getEstado(), getRol());
        if(us==null)
            app.abrirMensajeError("No se encontraron usuarios.", Interfaces.BUSCAR_USUARIOS);
        else
            app.abrirBuscarUsuarios2(us, Interfaces.BUSCAR_USUARIOS);
    }//GEN-LAST:event_tipoperActionPerformed

    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        app.volver(Interfaces.PRINCIPAL_GESTOR);
    }//GEN-LAST:event_cancelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField apellidos;
    private javax.swing.JButton cancel;
    private javax.swing.JTextField dni;
    private javax.swing.JComboBox estado;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField nombre;
    private javax.swing.JComboBox tipomed;
    private javax.swing.JComboBox tipoper;
    private javax.swing.JComboBox tipousu;
    // End of variables declaration//GEN-END:variables

}
